package com.yiyi.credit.manage.abutment.service;

import com.yiyi.credit.manage.abutment.common.enums.ResponseCode;
import com.yiyi.credit.manage.abutment.common.exceptions.ServiceException;
import com.yiyi.credit.manage.abutment.mapper.ProjectInfoMapper;
import com.yiyi.credit.manage.abutment.mapper.ThirdPartyMapper;
import com.yiyi.credit.manage.model.entity.ProjectInfo;
import com.yiyi.credit.manage.model.entity.SubStation;
import com.yiyi.credit.manage.model.entity.ThirdParty;
import com.yiyi.credit.manage.model.po.ThirdPartyPo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;


@Service
public class ProjectInfoService {
    private static final Logger logger = LoggerFactory.getLogger(ProjectInfoService.class);

    @Autowired
    private ProjectInfoMapper projectInfoMapper;

    @Autowired
    private ThirdPartyMapper thirdPartyMapper;

    public List<ThirdParty> queryThirdPartyList() throws ServiceException {
        return thirdPartyMapper.selectAll();
    }

    public List<ProjectInfo> getProjectInfoList() throws ServiceException {
        Example example = new Example(ProjectInfo.class);
        return projectInfoMapper.selectByExample(example);
    }

    public List<ProjectInfo> findProjectInfoByIds(Collection<Long> ids) {
        if (CollectionUtils.isEmpty(ids))
            return Collections.emptyList();

        Example example = new Example(SubStation.class);
        example.createCriteria().andIn("id", ids);
        return projectInfoMapper.selectByExample(example);
    }


    public List<ProjectInfo> getProjectInfoPageList(ProjectInfo projectInfo) throws ServiceException {
        return projectInfoMapper.getProjectInfoPageList(projectInfo);
    }


    public int addProjectInfo(ProjectInfo projectInfo) throws ServiceException {
        List<ProjectInfo> list = getProjectInfoByName(projectInfo.getName());
        if (null != list && list.size() > 0) {
            throw new ServiceException(ResponseCode.SUB_STATION_EXIST);
        }

        projectInfo.setCreateTime(new Date());
        projectInfo.setUpdateTime(new Date());
        return projectInfoMapper.insertSelective(projectInfo);
    }


    public int updateProjectInfo(ProjectInfo projectInfo) throws ServiceException {

        ProjectInfo checkProjectInfo = projectInfoMapper.selectByPrimaryKey(projectInfo.getId());
        if (checkProjectInfo == null) {
            throw new ServiceException(ResponseCode.SUB_STATION_NO_EXIST);
        }
        projectInfo.setUpdateTime(new Date());
        return projectInfoMapper.updateByPrimaryKeySelective(projectInfo);
    }


    public int deleteProjectInfo(ProjectInfo projectInfo) throws ServiceException {

        ProjectInfo checkprojectInfo = getProjectInfoById(projectInfo.getId());
        if (checkprojectInfo == null) {
            throw new ServiceException(ResponseCode.SERVICE_ERR);
        }
        return projectInfoMapper.deleteByPrimaryKey(projectInfo);
    }


    public int changeProjectInfoStatus(ProjectInfo projectInfo) throws ServiceException {

        ProjectInfo checkprojectInfo = getProjectInfoById(projectInfo.getId());
        if (checkprojectInfo == null) {
            throw new ServiceException(ResponseCode.SERVICE_ERR);
        }
        checkprojectInfo.setStatus(projectInfo.getStatus());
        return projectInfoMapper.updateByPrimaryKeySelective(checkprojectInfo);
    }


    public List<ProjectInfo> getProjectInfoByName(String name) throws ServiceException {

        Example example = new Example(ProjectInfo.class);
        example.createCriteria().andEqualTo("name", name);
        return projectInfoMapper.selectByExample(example);
    }


    public ProjectInfo getProjectInfoById(String id) throws ServiceException {

        return projectInfoMapper.selectByPrimaryKey(id);
    }


    public List<ProjectInfo> getSubStationByIds(String ids) throws ServiceException {

        String[] checkIds = ids.split(",");
        List<ProjectInfo> result = new ArrayList<>();
        for (int i = 0; i < checkIds.length; i++) {
            Long projectInfoId = Long.parseLong(checkIds[i]);
            ProjectInfo projectInfo = projectInfoMapper.selectByPrimaryKey(projectInfoId);
            result.add(projectInfo);
        }
        return result;
    }

    public Map<String, ProjectInfo> findByIds(Collection<String> ids) {
        if (CollectionUtils.isEmpty(ids))
            return Collections.emptyMap();

        Example stationMapper = new Example(ProjectInfo.class);
        stationMapper.createCriteria().andIn("id", ids);
        return projectInfoMapper.selectByExample(stationMapper)
                .stream()
                .collect(Collectors.toMap(ProjectInfo::getId, s -> s));
    }

}
